*This file creates Figure 1 in "Ambiguity and Enforcement" by Evan Calford and Gregory DeAngelo 

/* 

Input files
1. fines_taxes - share of government tax revenues that come from law enforcement fines and fees
2. marked_unmarked - number of marked and unmarked patrol vehicles in each law enforcement agency

Input files
1. figure 1 - Share of Government Revenues from Law Enforcement Fines and Fees & the Use of Unmarked Police Vehicles


*/


clear all


import delimited "fines_taxes.csv"

replace state = "AL" if state=="Alabama"
replace state = "AK" if state=="Alaska"
replace state = "AZ" if state=="Arizona"
replace state = "AR" if state=="Arkansas"
replace state = "CA" if state=="California"
replace state = "CO" if state=="Colorado"
replace state = "CT" if state=="Connecticut"
replace state = "DE" if state=="Delaware"
replace state = "FL" if state=="Florida"
replace state = "GA" if state=="Georgia"
replace state = "HI" if state=="Hawaii"
replace state = "ID" if state=="Idaho"
replace state = "IL" if state=="Illinois"
replace state = "IN" if state=="Indiana"
replace state = "IA" if state=="Iowa"
replace state = "KS" if state=="Kansas"
replace state = "KY" if state=="Kentucky"
replace state = "LA" if state=="Louisiana"
replace state = "ME" if state=="Maine"
replace state = "MD" if state=="Maryland"
replace state = "MA" if state=="Massachusetts"
replace state = "MI" if state=="Michigan"
replace state = "MN" if state=="Minnesota"
replace state = "MS" if state=="Mississippi"
replace state = "MO" if state=="Missouri"
replace state = "MT" if state=="Montana"
replace state = "NE" if state=="Nebraska"
replace state = "NV" if state=="Nevada"
replace state = "NH" if state=="New Hampshire"
replace state = "NJ" if state=="New Jersey"
replace state = "NM" if state=="New Mexico"
replace state = "NY" if state=="New York"
replace state = "NC" if state=="North Carolina"
replace state = "ND" if state=="North Dakota"
replace state = "OH" if state=="Ohio"
replace state = "OK" if state=="Oklahoma"
replace state = "OR" if state=="Oregon"
replace state = "PA" if state=="Pennsylvania"
replace state = "RI" if state=="Rhode Island"
replace state = "SC" if state=="South Carolina"
replace state = "SD" if state=="South Dakota"
replace state = "TN" if state=="Tennessee"
replace state = "TX" if state=="Texas"
replace state = "UT" if state=="Utah"
replace state = "VT" if state=="Vermont"
replace state = "VA" if state=="Virginia"
replace state = "WA" if state=="Washington"
replace state = "WV" if state=="West Virginia"
replace state = "WI" if state=="Wisconsin"
replace state = "WY" if state=="Wyoming"
rename state state_fine
rename place city
replace city = lower(city)
gen fine_n = _n
save "fines_taxes.dta", replace

clear
import delimited "marked_unmarked.csv", clear
drop year
replace city = lower(city)
gen car_n = _n
save "marked_unmarked.dta", replace

matchit car_n city using "fines_taxes.dta", idu(fine_n) txtu(city) threshold(0.2)
save "matched_car_fine.dta", replace


clear
use "matched_car_fine.dta"
bysort car_n: egen max = max(similscore)
gen diff= similscore-max

keep if diff<0.01 & diff > -0.01
keep city city1 fine_n car_n
save "matches.dta", replace

clear
use "marked_unmarked.dta"
merge 1:m car_n using "matches.dta", gen(match_merge)
drop if match_merge<3
rename city city_car
rename city1 city
merge m:1 city fine_n using "fines_taxes.dta", gen(fine_merge)
drop if fine_merge<3
drop *_merge

sort car_n fine_n
order state*
drop if state!=state_fine

**generate a variable that is the percent of taxes from fines
gen fine_tax_ratio = finespercapita/taxpercapita
egen state_num = group(state)
destring unmarkedcars, force replace
destring markedcars, force replace

collapse fine_tax_ratio (sum) markedcars unmarkedcars population, by(city state)
gen perc_undercover = unmarkedcars/(markedcars+unmarkedcars)

drop if perc_undercover==0
drop if population>150000
gen county = regexm(city, "county")
drop if county==1
gen totalcars = markedcars+ unmarkedcars
gen count = 1

replace fine_tax_ratio = round(fine_tax_ratio, .005)

*dropping ratios that are inconceivably high that they are errors
drop if fine_tax_ratio>0.25
drop if perc_undercover>0.20

collapse perc_undercover (sum) count, by(fine_tax_ratio)

twoway scatter perc_undercover fine_tax_ratio [w=count], msymbol(circle_hollow) ///
|| lfit perc_undercover fine_tax_ratio ///
 ,title("Use of Undercover Police Vehicles by Fine-to-Tax Ratio") ytitle("Percent Undercover Vehicles") xtitle("Fine to Tax Ratio") ////
		 legend(off) plotregion(fcolor(white)) graphregion(fcolor(white))

